package com.wnhz.work.yz.adapter;


import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.wnhz.work.yz.R;
import com.wnhz.work.yz.activity.SellAfterApplyActivity;
import com.wnhz.work.yz.base.BaseRVAdapter;
import com.wnhz.work.yz.base.BaseViewHolder;
import com.wnhz.work.yz.bean.SellAfterApplyBean;

import org.xutils.x;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by Administrator on 2017/2/23 0023.
 */

public class SellAfterAdapter extends BaseAdapter {
    private Context context;
    List<SellAfterApplyBean.InfoEntity> listBean = new ArrayList<>();

    public SellAfterAdapter(Context context, List<SellAfterApplyBean.InfoEntity> beanList) {
        this.context = context;
        this.listBean = beanList;
    }


    @Override
    public int getCount() {
        return listBean.size();
    }

    @Override
    public Object getItem(int position) {
        return listBean.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        SellAfterGoodsViewHolder holder = new SellAfterGoodsViewHolder();
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_sell_after_goods_listview, null);
            holder.tv_good_code = (TextView) convertView.findViewById(R.id.tv_good_code);
            holder.tv_good_time = (TextView) convertView.findViewById(R.id.tv_good_time);
            holder.iv_goods = (ImageView) convertView.findViewById(R.id.iv_goods);
            holder.tv_good_name = (TextView) convertView.findViewById(R.id.tv_good_name);
            holder.tv_goods_num = (TextView) convertView.findViewById(R.id.tv_goods_num);
            holder.state = (TextView) convertView.findViewById(R.id.state);
            holder.recycler = (RecyclerView) convertView.findViewById(R.id.recycler);
            convertView.setTag(holder);
        } else {

            holder = (SellAfterGoodsViewHolder) convertView.getTag();

        }
        holder.tv_good_code.setText(listBean.get(position).getOrder_no());
        holder.state.setText(listBean.get(position).getOrder_desc());
        holder.tv_good_time.setText(listBean.get(position).getOrder_time());
        final List<SellAfterApplyBean.InfoEntity.DataEntity> data = listBean.get(position).getData();
        holder.recycler.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));
        final String order_id = listBean.get(position).getOrder_id();
        holder.recycler.setAdapter(new BaseRVAdapter(context, data) {
            @Override
            public int getLayoutId(int viewType) {
                return R.layout.sell_item_data;
            }

            @Override
            public void onBind(BaseViewHolder holder, final int position) {
                x.image().bind(holder.getImageView(R.id.iv_goods), data.get(position).getGoods_spic());
                holder.setTextView(R.id.tv_good_name, data.get(position).getGoods_name());
                holder.setTextView(R.id.tv_goods_num, "数量*" + data.get(position).getGoods_nums());
                holder.getView(R.id.btn_sellafter).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(context, SellAfterApplyActivity.class);
                        SellAfterApplyBean.InfoEntity.DataEntity data2 = data.get(position);
                        intent.putExtra("data", data2);
                        intent.putExtra("order_id", order_id);
                        context.startActivity(intent);
                    }
                });

            }
        });

        return convertView;
    }


    class SellAfterGoodsViewHolder {

        TextView tv_good_code;
        TextView tv_good_time;
        ImageView iv_goods;
        TextView tv_good_name;
        TextView tv_goods_num;
        TextView state;
        RecyclerView recycler;
    }


}
